package com.wxit.algorithm.practice;

import java.util.Arrays;

public class Fibonacci {

    public static void main(String[] args) {
        System.out.println(fibonacci(12));
    }

    public static int fibonacci(int n) {
//        return m1(n);
        int[] cache = new int[n + 1];
        Arrays.fill(cache, -1);
        cache[0] = 0;
        cache[1] = 1;
        return f(n, cache);
    }

    private static int m1(int n) {
        if (n == 0) {
            return 0;
        }

        if (n == 1) {
            return 1;
        }

        int x = fibonacci(n - 1);
        int y = fibonacci(n - 2);
        return x + y;
    }

    /**
     * 记忆法
     * @param n
     * @param cache
     * @return
     */
    private static int f(int n, int[] cache) {
        if (cache[n] != -1) {
            return cache[n];
        }

        int x = f(n - 1, cache);
        int y = f(n - 2, cache);
        cache[n] = x + y;
        return cache[n];
    }
}
